<template>
  <n-modal v-model:show="showModal" preset="dialog" :show-icon="false" :onAfterLeave="reset">
    <template #header>
      <div>{{ title }}</div>
    </template>
    <n-card v-if="step===1">
      <div>{{ delname }}</div>
      <template #action>
        <div class="w-full h-10 flex justify-end items-center">
          <n-button @click="doClose">取消</n-button>
          <n-button @click="handleOK" type="primary">确定</n-button>
        </div>
      </template>
    </n-card>
    <n-card v-if="step===2">
      <n-result status="success" title="删除成功" :description="formValue.id?'编辑成功':'创建成功'"></n-result>
    </n-card>
    <n-card v-if="step===3">
      <n-result status="error" title="删除错误" >
        <template>
          <p>{{ errortext }}</p>
        </template>
      </n-result>
      <template #action>
        <div class="w-full h-10 flex justify-end items-center">
          <n-button @click="doClose">关闭</n-button>
        </div>
      </template>
    </n-card>
  </n-modal>
</template>

<script setup name="Edit">
import {useRoute, useRouter} from "vue-router";

const router = useRouter()
const route = useRoute()
const showModal = ref(false)
const title = ref("删除")
const step = ref(1)   // 步骤，1是提示删除，2是删除成功，3是删除失败
const delname = ref("")
const itemid = ref("")
const delfunc = ref(null)
const errortext=ref("")
const init = (delfunc, itemid, name = null) => {
  itemid.value = itemid
  delfunc.value = delfunc
  if (name) {
    delname.value = `是否删除${name}？`
  } else {
    delname.value = `是否删除本条数据？`
  }
  step.value = 1
  showModal.value = true
}
const doClose = () => {
  showModal.value = false
}
const reset = () => {
  itemid.value = ""
  delname.value = ""
  delfunc.value = null
  errortext.value = ""
}
// 点击确定
const handleOK = () => {
  deletefunc.value(itemid.value, getsuccess, getfaild)
}

// 删除成功
const getsuccess = () => {
  step.value = 2
  setTimeout(() => {
    doClose()
  }, 500)
}
// 删除失败
const getfaild = () => {
  step.value = 3
}

defineExpose({init, doClose, reset})
</script>

<style scoped>

</style>